//
//  ZYCTeachingTeachLearningPaperContentCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/2.
//

#import "ZYCTeachingTeachLearningPaperContentCell.h"
#import "ZYCTeachLearningPaperModel.h"
#define maxContent 200

#define ZYCContentPlaceholder @"请输入不超过200字符数的回答~"
#define ZYCContentPlaceholderColor [UIColor colorWithHexString:@"#999999" alpha:1.0]

@interface ZYCTeachingTeachLearningPaperContentCell ()<UITextViewDelegate>
@property (nonatomic, strong) UILabel *questionLabel;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UITextView *contentTextView;


@end
@implementation ZYCTeachingTeachLearningPaperContentCell

- (void)setModel:(ZYCSubjectModel *)model{
    _model = model;
    _questionLabel.attributedText = [self returnFuwenBen:[NSString stringWithFormat:@"%@、%@ %@",model.sortNum,model.subjectTypeStr,model.subjectTitle]];
    _contentTextView.text = model.answerContent;
    
}

- (NSMutableAttributedString *)returnFuwenBen:(NSString *)string{
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange rang0 = NSMakeRange(0, self.model.sortNum.length);
    [attriString addAttribute:NSFontAttributeName value:kCustomFont(@"PingFangSC-Medium", 16.0f) range:rang0];
    NSRange rang1 = NSMakeRange(self.model.sortNum.length + 1, self.model.subjectTypeStr.length + 1);
    [attriString addAttribute:NSForegroundColorAttributeName value:HEX_RGB(@"#39A85B") range:rang1];
    return attriString;;
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.questionLabel];
        [self.contentView addSubview:self.line];
        [self.contentView addSubview:self.contentTextView];
        [self.questionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(14.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(14.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
            make.top.equalTo(self.questionLabel.mas_bottom).offset(14.0f);
            make.height.mas_equalTo(0.5f);
        }];
        
        [self.contentTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.line.mas_bottom).offset(16.0f);
            make.left.equalTo(self.contentView).offset(14.0f);
            make.height.mas_equalTo(250.0f);
            make.right.bottom.equalTo(self.contentView).offset(-14.0f);
        }];

    }
    return self;
}

#pragma mark - UITextViewDelegate
-(void) textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length < 1) {
        textView.text = ZYCContentPlaceholder;
        textView.textColor = ZYCContentPlaceholderColor;
        self.model.answerContent = @"";
        self.model.isPaper = NO;
    }else{
        self.model.answerContent = textView.text;
        self.model.isPaper = YES;
        if (self.selectModelBlock) {
            self.selectModelBlock(self.model);
        }
    }
}

-(void) textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:ZYCContentPlaceholder]) {
        textView.text = @"";
        textView.textColor = ZYCContentPlaceholderColor;
    }else{
        textView.textColor = HEX_RGB(@"#2B302D");
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    return textView.text.length + (text.length - range.length) <= 200;
}
 


- (UILabel *) questionLabel{
    if (!_questionLabel) {
        _questionLabel = [[UILabel alloc] init];
        _questionLabel.textAlignment = NSTextAlignmentLeft;
        _questionLabel.font = kCustomFont(@"PingFangSC-Light", 16.0f);
        _questionLabel.numberOfLines = 0;
    }
    return _questionLabel;
}

- (UILabel *)line {
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EBEBEB");
    }
    return _line;
}


- (UITextView *)contentTextView {
    if (!_contentTextView) {
        _contentTextView = [[UITextView alloc] init];
        _contentTextView.delegate = self;
        _contentTextView.font = kCustomFont(@"PingFangSC-Light", 16.0f);
        _contentTextView.text = ZYCContentPlaceholder;
        _contentTextView.textColor = HEX_RGB(@"#2B302D");
        _contentTextView.scrollEnabled = NO;
    }
    return _contentTextView;
}
@end
